package com.atguigu.gulimall.product.controller;

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;

// import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.atguigu.common.utils.QiniuOSSUtils;
import com.atguigu.common.valid.group.AddGroup;
import com.atguigu.common.valid.group.UpdateGroup;
import com.atguigu.common.valid.group.UpdateStatusGroup;
import com.baomidou.mybatisplus.extension.injector.methods.AlwaysUpdateSomeColumnById;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.BrandEntity;
import com.atguigu.gulimall.product.service.BrandService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import org.springframework.web.multipart.MultipartFile;


/**
 * 品牌
 *
 * @author leifengyang
 * @email sunlightcs@gmail.com
 * @date 2022-10-22 22:12:21
 */
@Slf4j
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    @Value("${upload.suffixes}")
    private String suffixes;

    @PostMapping("/upload")
    public R upload(MultipartFile file) throws IOException {
        log.info(file.toString());

        // 参数校验
        if(Objects.isNull(file)) {
            return R.error("文件不能为空");
        }

        // 文件格式限制
        boolean canUpload = false;
        String filename = file.getOriginalFilename();
        String[] suffixeArray = suffixes.split(", ");
        for (String suffix : suffixeArray) {
            if(filename.endsWith(suffix)) {
                canUpload = true;
                break;
            }
        }
        if(!canUpload) {
            throw new RuntimeException("上传图片只支持 png、jpeg、jpg 格式！");
        }

        // 上传文件到OSS
        String fileName = QiniuOSSUtils.uploadFile(file.getInputStream());
        String fileAddress = QiniuOSSUtils.BASE_URL + fileName;

        // todo: 上传成功, 将文件放入到垃圾缓存中
        // ImageUtil.addToRubishPicCache(redisTemplate, fileAddress);

        return R.ok().put("imageAddress", fileAddress);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    // @RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:brand:save")
    public R save(@Validated({AddGroup.class}) @RequestBody BrandEntity brand){
		brandService.save(brand);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:brand:update")
    public R update(@Validated({UpdateGroup.class}) @RequestBody BrandEntity brand){
		brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 修改品牌的显示状态
     * @param brand
     * @return
     */
    @RequestMapping("update/status")
    public R updateStatus(@Validated({UpdateStatusGroup.class}) @RequestBody BrandEntity brand) {
        brandService.updateById(brand);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
